package com.graduate.twentyone.service.Impl;

import com.graduate.twentyone.domain.Sale;
import com.graduate.twentyone.domain.Users;
import com.graduate.twentyone.mapper.SaleMapper;
import com.graduate.twentyone.mapper.UsersMapper;
import com.graduate.twentyone.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    SaleMapper saleMapper;

    @Autowired
    UsersMapper usersMapper;

    public Integer addsale(Sale sale) {
        return saleMapper.addsale(sale);
    }

    public List<Sale> selectsale(Sale sale) {

//        获取数据
        List<Sale> selectsale = saleMapper.selectsale(sale);

        ArrayList<Sale> saleArrayList = new ArrayList<>();

        ArrayList<Sale> arrayList = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < selectsale.size(); i++) {
//            System.out.println("j = " + j);

            Sale sale1 = new Sale();
//          第一条数据直接插入
            if (i == 0) {
                arrayList.add(selectsale.get(i));
            } else {
//                时间相同就插入
                if (selectsale.get(i - 1).getDatetime().equals(selectsale.get(i).getDatetime())) {
                    arrayList.add(selectsale.get(i));
                } else {
//                    不同就把之前的先存在大集合里面,之后小集合什么的变空,并把该次的数据放入
                    sale1.setSaleList(arrayList);
                    sale1.setDatetime(selectsale.get(i - 1).getDatetime());
                    sale1.setUid(selectsale.get(i - 1).getUid());
//                  如果当前行的时间和现实的时间是一天，则fname是true 前端只显示这一天的数据
                    if (selectsale.get(i - 1).getDatetime().toString().substring(0, 10).equals(new Date().toString().substring(0, 10))) {
                        sale1.setFname("true");
                    }
//                    设置uname
                    if (selectsale.get(i).getUid() != 1) {
                        sale1.setTname(usersMapper.selectbyUid(selectsale.get(i).getUid()).getTname());
                    }

//                   修改订单结算状态
                    saleArrayList.add(sale1);
                    if (selectsale.get(i - 1).getMoneyStatus() == 1) {
                        saleArrayList.get(j).setMoneyStatus(1);
                    }
//                    修改订单完成状态
                    if (selectsale.get(i - 1).getStatus() == 2) {
                        saleArrayList.get(j).setStatus(2);
                    } else if (selectsale.get(i - 1).getStatus() == 3) {
                        saleArrayList.get(j).setStatus(3);
                    }
                    j++;

                    sale1 = new Sale();
                    arrayList = new ArrayList<Sale>();

                    arrayList.add(selectsale.get(i));
                }
            }
//           如果是最后一条数据，就把数据放入并且把小集合添加到大集合
            if (i == selectsale.size() - 1) {
                sale1.setSaleList(arrayList);
                sale1.setDatetime(selectsale.get(i).getDatetime());
                sale1.setUid(selectsale.get(i).getUid());
                saleArrayList.add(sale1);
                if (selectsale.get(i - 1).getMoneyStatus().equals(1)) {
                    saleArrayList.get(j).setMoneyStatus(1);
                }
                if (selectsale.get(i - 1).getStatus() == 2) {
                    saleArrayList.get(j).setStatus(2);
                } else if (selectsale.get(i - 1).getStatus() == 3) {
                    saleArrayList.get(j).setStatus(3);
                }
            }
        }
        System.out.println("saleArrayList = " + saleArrayList);
        return saleArrayList;
    }

    public List<Sale> findsale(Sale sale) {
        return saleMapper.selectsale(sale);
    }

    public Integer updatesale(Sale sale) {
        return saleMapper.updatesale(sale);
    }

}
